#include <iostream>
#include <signal.h>
#include <unistd.h>
#include <cstring>
#include <sys/wait.h>
#include <sys/types.h>
using namespace std;
pid_t pid=0;
/*void handle(int signo)
{
    //回收对应的子进程
    waitpid(pid,nullptr,0);
    exit(2);
}*/
int main()
{
    signal(SIGCHLD,SIG_IGN);//当父进程收到子进程发来的信号时，进行捕捉
    pid=fork();
    if(pid==0)
    {
        //子进程
        while(1);
        {
            //让子进程不终止
        }
    }
    cout<<pid<<endl;
    while (1)
    {
        sleep(1);
        cout<<"我是父进程，我在进行工作...."<<endl;
    }
    return 0;
}





/*int flag=0;
void handle(int)
{
    cout<<"change flag 0 to 1"<<endl;;
    flag=1;
}
int main()
{
    signal(2,handle);
    while(!flag);
    cout<<"The process exits success";
    return 0;
}*/


/*void handle2(int)
{
    cout<<"我是2号信号"<<endl;
    int cnt=20;
    while(cnt--)
    {
        cout<<"2号信号"<<cnt<<endl;
    sleep(1);;
    }
}
void handle3(int)
{
    cout<<"我是3号信号"<<endl;
     int cnt=20;
     while(cnt--)
    {
        cout<<"3号信号"<<cnt<<endl;
    sleep(1);;
    }
}

int main()
{
    cout << "pid:" << getpid() << endl;
    signal(2,handle2);
    signal(3,handle3);
    while(1);
    return 0;
}*/

/*void handle(int signo)
{
    sigset_t set;
    cout << "我是被捕捉的" << signo << "号信号" << endl;
    int cnt = 30;
    while (cnt--)
    {
    sigpending(&set);
        for (int i = 1; i <= 31; i++)
        {
            // 打印一下对应的pending表
            if (sigismember(&set, i))
            {
                cout << 1;
            }
            else
                cout << 0;
        }
        cout<<endl;
        sleep(1);
    }
}
int main()
{
    cout << "pid:" << getpid() << endl;
    sigset_t set;
    sigaddset(&set, 3);
    sigaddset(&set, 4);
    sigaddset(&set, 5);
    struct sigaction act;
    struct sigaction oact;
    memset(&act, 0, sizeof(act));
    act.sa_handler = handle;
    act.sa_mask = set; // 在执行2号命令的处理函数时，同时也屏蔽掉3,4,5号信号
    memset(&oact, 0, sizeof(oact));
    sigaction(2, &act, &oact);
    while (1)
    {
    }
    return 0;
}*/

/*void handle(int signo)
{

    sigset_t set;
    while (1)
    {
        cout<<"pending:";
        cout<<signo<<endl;
            sigpending(&set);
        sleep(1);
        for (int i = 1; i <= 31; i++)
        {
            if (sigismember(&set, i))
            {
                cout << 1;
            }
            else
            {
                cout << 0;
            }
        }
        cout << endl;
    }
    cout << "我是对应的" << signo << "信号" << endl;
}

int main()
{
    cout << "pid" << getpid() << endl;
    signal(2, handle);
    //signal(3, handle);
    //signal(4, handle);
    //signal(5, handle);
    // sigset_t set,oset;
    // sigemptyset(&set);//初始化
    // sigemptyset(&oset);
    // sigaddset(&set,2);
    // sigprocmask(SIG_SETMASK,&set,&oset);//屏蔽掉2号信号
    while (1)
    {
        cout << "我是一个进程，我在运行......" << endl;
        sleep(1);
    }
    return 0;
}*/

/*int main()
{
    cout<<"pid："<<getpid()<<endl;
    for(int i=1;i<=31;i++)
    {
    signal(i,handle);
    }
    while (1)
    {
        cout << "我是一个进程，我在执行。。。" << endl;
        sleep(1);
    }
    return 0;
}*/